<!DOCTYPE html>
<html>

<head>
    <script src="./sm2.js"></script>
    <script>
        var pubKey;
        var cipherMode = 1; // 1 - C1C3C2，0 - C1C2C3，默认为1
        function combinePubkey() {
            var x = document.getElementById("x").value;
            var y = document.getElementById("y").value;
            pubKey = "04" + x + y;
            document.getElementById("pubKey").innerText = pubKey;
        }

        function encrypt() {
            if (pubKey === undefined) {
                alert("需要先提供密钥");
                return;
            }
            var plainText = document.getElementById("inPlainText").value;
            var encryptData = sm2.doEncrypt(plainText, pubKey, cipherMode);
            document.getElementById("encryptedText").value = encryptData;
        }
    </script>
</head>

<body>
    根据公钥分量x和y组合后，进行加密<br>
    <form>
        <fieldset>
            <legend>第一步: 组合公钥</legend>
            <tr>
                <td>
                    <p>公钥明文分量X(Hex)(长度64字符):</p>
                </td>
                <td>
                    <input id="x" style="width:650px;"
                        value="ad3e659920b5cdee35560230cfe744a045201ed21a70fc2c45f7bbb62bbb17b4"><br>
                </td>
            </tr>
            <tr>
                <td>
                    <p>公钥明文分量Y(Hex)(长度64字符):</p>
                </td>
                <td>
                    <input id="y" style="width:650px;"
                        value="1d6020cc5a0fb0c23b26543d7b18b23d485e53e60b9eb8dbb3ce81624249ed86"><br>
                </td>
            </tr>
            <tr>
                <td>
                    <button type="button" onclick="combinePubkey()">组合公钥</button><br>
                </td>
            </tr>
            <tr>
                <td>
                    <textarea id="pubKey" style="width: 650px;height: 30px;"></textarea><br>
                </td>
            </tr>
        </fieldset>
        <fieldset>
            <legend>2. 加密</legend>
            <tr>
                <td>明文:</td>
                <td>
                    <input id="inPlainText" value="123456" style="width: 650px;"><br>
                </td>
            </tr>
            <tr>
                <td>
                    <button type="button" onclick="encrypt()">加密</button><br>
                </td>
            </tr>
            <tr>
                <td>
                    <p>密文:</p>
                </td>
                <td>
                    <textarea id="encryptedText" style="width: 650px;height: 100px;"></textarea>
                </td>
            </tr>
        </fieldset>
    </form>
</body>

</html>